package com.adtec.base.cloud.baseservice.rbac.system.service;


import com.adtec.base.cloud.baseservice.rbac.config.DbLoadSysConfig;
import com.adtec.base.cloud.baseservice.rbac.feign.SmsService;
import com.adtec.base.cloud.commons.exception.AjaxResponse;
import com.adtec.base.cloud.commons.exception.CustomException;
import com.adtec.base.cloud.commons.exception.CustomExceptionType;
import com.adtec.base.cloud.persistence.system.mapper.MySystemMapper;
import com.adtec.base.cloud.persistence.system.mapper.SysUserMapper;
import com.adtec.base.cloud.persistence.system.model.SysUser;
import com.adtec.base.cloud.persistence.system.model.SysUserOrg;
import com.adtec.base.cloud.security.jwt.utils.GetUserId;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Date;

import static com.adtec.base.cloud.commons.cachekey.RBACCacheKey.USER_DETAIL;

@Service
@Slf4j
public class SysUserService {

    @Resource
    private SmsService smsService;

    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private MySystemMapper mySystemMapper;
    @Resource
    private PasswordEncoder passwordEncoder;
    @Resource
    private DbLoadSysConfig dbLoadSysConfig;
//    @Value("${user.init.password}")
//    private String defaultPwd;

    //    @Resource
//    private JwtTokenUtil jwtTokenUtil;


    //刷新内存配置
    private void refreshDbLoadSysConfig() {
        try {
            log.info("刷新内存配置");
            dbLoadSysConfig.run();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Resource
    private GetUserId getUserId;

    //根据登录用户名查询用户信息
    public SysUser getUserByUserName(String userName) {
        Assert.isTrue(StringUtils.isNotEmpty(userName),
                "查询参数用户名不存在");

        SysUser sysUser = sysUserMapper.selectOne(
                new QueryWrapper<SysUser>().eq("username", userName));
        if (sysUser != null) {
            sysUser.setPassword("");  //清空密码信息
        }
        return sysUser;
    }

    //用户管理：查询
    public IPage<SysUserOrg> queryUser(Long orgId,
                                       String username,
                                       String phone,
                                       String email,
                                       Boolean enabled,
                                       Date createStartTime,
                                       Date createEndTime,
                                       Integer pageNum,
                                       Integer pageSize) {
        Page<SysUserOrg> page = new Page<>(pageNum, pageSize);   //查询第pageNum页，每页pageSize条数据
        if (StringUtils.isEmpty(username) && orgId == null) { //获取当前登录用户的orgId
            SysUser sysUser = sysUserMapper.selectOne(
                    new QueryWrapper<SysUser>()
                            .eq("username", getUserId.getUsernameFromHeader())
            );
            if (sysUser != null) {
                orgId = sysUser.getOrgId();
            }
        }
        //查询orgId组织及其自组织的用户列表
        return mySystemMapper.selectUser(
                page,
                orgId,
                username,
                phone, email, enabled,
                createStartTime,
                createEndTime);
    }

    //用户管理：修改
    @CacheEvict(value = USER_DETAIL, key = "#sysuser.getUsername()")
    public void updateUser(SysUser sysuser) {
        Assert.isTrue(sysuser.getId() != null,
                "修改操作必须带主键");

        sysUserMapper.updateById(sysuser);
    }

    //用户管理：新增
    public void addUser(SysUser sysuser) {
        this.refreshDbLoadSysConfig();
        sysuser.setPassword(passwordEncoder.encode(
                dbLoadSysConfig.getConfigItem("user.init.password")
        ));
        //sysuser.setCreateTime(new Date());  //创建时间
        sysuser.setEnabled(true); //新增用户激活
        sysUserMapper.insert(sysuser);
    }

    //用户管理：删除
//    public void deleteUser(Long userId) {
//        Assert.isTrue(userId != null, "删除操作必须带主键");
//        sysUserMapper.deleteById(userId);
//    }

    //用户管理：删除
    @CacheEvict(value = USER_DETAIL, key = "#username")
    public void deleteUser(String username) {
        Assert.isTrue(org.apache.commons.lang.StringUtils.isNotEmpty(username), "删除操作必须带主键");

        //根据用户名删除用户信息
        LambdaQueryWrapper<SysUser> lambdaQ = Wrappers.lambdaQuery();
        lambdaQ.eq(SysUser::getUsername, username);
        sysUserMapper.delete(lambdaQ);
    }

    //用户管理：重置密码
    @CacheEvict(value = USER_DETAIL, key = "#username")
    public void pwdreset(String username) {
        Assert.isTrue(org.apache.commons.lang.StringUtils.isNotEmpty(username), "重置密码操作必须带主键");

        QueryWrapper<SysUser> sysUserQueryWrapper = new QueryWrapper<>();
        sysUserQueryWrapper.eq("username", username);
        SysUser sysUser = sysUserMapper.selectOne(sysUserQueryWrapper);
        String defaultPwd = dbLoadSysConfig.getConfigItem("user.init.password");
        sysUser.setPassword(passwordEncoder.encode(defaultPwd));

        String phone = sysUser.getPhone();
        if (StringUtils.isEmpty(phone)) {
            throw new CustomException(CustomExceptionType.SYSTEM_ERROR,
                    "手机号为空,请维护手机号信息");
        }

        //根据用户名修改用户信息
//        LambdaQueryWrapper<SysUser> lambdaQ = Wrappers.lambdaQuery();
//        lambdaQ.eq(SysUser::getUsername, username);
//        sysUserMapper.update(sysUser, lambdaQ);
        sysUserMapper.updateById(sysUser);

        AjaxResponse ajaxResponse = smsService.send(phone, "您好，管理员已经将您的密码重置为" + defaultPwd);
        if (!ajaxResponse.isIsok()) {
            throw new CustomException(CustomExceptionType.SYSTEM_ERROR,
                    ajaxResponse.getMessage());
        }
    }


    //判断当前登录的用户密码是否是默认密码，如果是，会让他去修改
    public Boolean isdefault(String username) {
        SysUser sysUser = sysUserMapper.selectOne(
                new QueryWrapper<SysUser>().eq("username", username));
        this.refreshDbLoadSysConfig();
        //判断数据库密码是否是默认密码
        return passwordEncoder.matches(
                dbLoadSysConfig.getConfigItem("user.init.password"),
                sysUser.getPassword());
    }

    //个人中心：修改密码
    @CacheEvict(value = USER_DETAIL, key = "#username")
    public void changePwd(String username, String oldPass, String newPass) {

        SysUser sysUser = sysUserMapper.selectOne(
                new QueryWrapper<SysUser>().eq("username", username));
        //判断旧密码是否正确
        boolean isMatch = passwordEncoder.matches(oldPass, sysUser.getPassword());
        Assert.isTrue(isMatch, "原密码输入错误，请确认后重新输入！");

        SysUser sysUserNew = new SysUser();
        sysUserNew.setId(sysUser.getId());
        sysUserNew.setPassword(passwordEncoder.encode(newPass));
        sysUserMapper.updateById(sysUserNew);
    }

    //用户管理：更新用户的激活状态
    @CacheEvict(value = USER_DETAIL, key = "#username")
    public void updateEnabled(String username, Boolean enabled) {
        Assert.isTrue(org.apache.commons.lang.StringUtils.isNotEmpty(username), "修改操作必须带主键");
        SysUser sysUser = new SysUser();
        sysUser.setEnabled(enabled);

        //根据用户名修改用户信息
        LambdaQueryWrapper<SysUser> lambdaQ = Wrappers.lambdaQuery();
        lambdaQ.eq(SysUser::getUsername, username);
        sysUserMapper.update(sysUser, lambdaQ);
    }

}
